<?php
/**
 * 后台Ajax处理
 */

class AjaxController extends Cms_AdminController {
	
	/**
	 * 初始化
	 */
	public function init() {
	
	}
	
	/**
	 * 当前位置
	 */
	public function posAction() {
		$menuid = $this->_request->getPost ( 'menuid' );
		echo Model_Menu::current_pos ( $menuid );
		exit ();
	}
	
	/**
	 * 异步检测emial合法性
	 */
	public function isemailAction() {
		$email = $this->_request->get ( 'email' );
		
		$admin = new DB_Admin ();
		$return = $admin->loadInfo ( '*', "email='{$email}'" );
		if ($return) {
			exit ( '0' );
		}
		exit ( '1' );
	}
	
	/**
	 * 检测管理员用户名是否存在
	 */
	public function checknameAction() {
		$username = $this->_request->get ( 'username' );
		
		$admin = new DB_Admin ();
		$return = $admin->loadInfo ( '*', "username='{$username}'" );
		if ($return) {
			exit ( '0' );
		}
		exit ( '1' );
	}
	
	/**
	 * 异步检测密码
	 */
	public function passwordAction() {
		$old_password = $this->_request->get ( 'old_password' );
		
		$admin = new DB_Admin ();
		$admin_info = $admin->loadInfoByPrimary ( $this->adminsession_info ['uid'] );
		
		if (strtolower ( md5 ( $old_password ) ) == strtolower ( $admin_info [password] )) {
			exit ( '1' );
		} else {
			exit ( '0' );
		}
	}
	
	/**
	 * 后台登陆处理
	 */
	public function loginAction() {
		
		$username = $this->_request->getPost ( 'username' );
		$password = $this->_request->getPost ( 'password' );
		$code = $this->_request->getPost ( 'code' );
		
		$Model_AdminSession = new Model_AdminSession ();
		if (! $Model_AdminSession->checkSecCode ( $code )) {
			$state = array ('state' => - 2, 'content' => '验证码错误!' );
			echo Zend_Json::encode ( $state );
			exit ();
		}
		
		$login_rs = null;
		$rm = 10800;
		
		$AdminSession = Cms_AdminSession::getInstance ();
		if ($AdminSession->login ( $username, $password, $rm, $login_rs )) {
			$state = array ('state' => 1, 'content' => '登陆成功!' );
			echo Zend_Json::encode ( $state );
			exit ();
		} else {
			$state = array ('state' => - 1, 'content' => '用户名或密码错误，请确认填写正确后重试!' );
			echo Zend_Json::encode ( $state );
			exit ();
		}
	}
}
?>